<template src="./template.html"></template>
<style src="./style.css" scoped></style>
<script>
    import api from "api/api";
    import modal from "utils/modal";
    import navigator from "utils/navigator";
    import route from "constants/route";
    import constant from "constants/constant";

    const globalEvent = weex.requireModule('globalEvent');

    export default{
        data(){
            return{
                rid: null,
                goods: {},
                recordList: [],
                pageLoaded: false
            }
        },
        created(){
            let self = this;
            let config = weex.config;
            if(config.rid) {
                self.rid = config.rid;
            }
            self.pageInit();

            //监听修改退款事件
            globalEvent.addEventListener(constant.EVENT_ORDER_MODIFY_REFUND, function (e) {
                self.pageInit();
            });

            //监听退货事件
            globalEvent.addEventListener(constant.EVENT_ORDER_RETURN_GOODS, function (e) {
                self.pageInit();
            });
        },
        methods: {
            pageInit(){
                let self = this;
                api.sendHttp('refundDetailApi',{rid:self.rid}).then(res=>{
                    self.pageLoaded = true;
                    self.goods = res.data;
                    self.recordList = res.data.records || [];
                },
                res=>{
                    modal.toast(res.msg || "获取退款详情失败");
                });
            },
            updateApplyRefund(){
                let self = this;
                let options = {
                    classType: constant.Drawer_Class_Type,
                    url: route.order_applyfund,
                    title: "修改退款",
                    params: {
                        goods: self.goods
                    }
                };
                navigator.push(options);
            },
            cancelApplyRefund(){
                let self = this;
                api.sendHttp("refundCancelApi",{rid:self.rid}).then(res=>{
                    modal.toast(res.msg || "撤销退款申请成功");
                    self.goods = res.data;
                    self.recordList = res.data.records || [];
                },
                res=>{
                    modal.toast(res.msg || "撤销退款申请失败");
                });
            },
            returnGoods(){
                let self = this;
                let options = {
                    classType: constant.Drawer_Class_Type,
                    url: route.order_applygoods,
                    title: "申请退货",
                    params: {
                        goods: self.goods
                    }
                };
                navigator.push(options);
            }
        },
        destroyed(){
            globalEvent.removeEventListener(constant.EVENT_ORDER_MODIFY_REFUND);
            globalEvent.removeEventListener(constant.EVENT_ORDER_RETURN_GOODS);
        }
    }
</script>
